package com.yanfan.energy.controller;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.energy.domain.*;
import com.yanfan.energy.domain.statist.WorkDateStatistics;
import com.yanfan.energy.service.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/energy/monitoring")
@Api(tags = "能源监控")
public class EquipmentConsumptionController {

    @Autowired
    private final EnergyTemplateService energyTemplateService;
    @Autowired
    private OverallEnergyService overallEnergyService;
    @Autowired
    private EquipmentConsumptionService equipmentConsumptionService;
    @Autowired
    private ExcelService excelService;
    @Resource
    private ConsumptionService consumptionService;

    //  *  ------------------------------------------------------------- 新的


    public EquipmentConsumptionController(EnergyTemplateService energyTemplateService) {
        this.energyTemplateService = energyTemplateService;
    }


//    Consumption   Statistics

    /**
     * x
     * 设备能源监控
     */
    @PostMapping("/consumptionStatistics")
    @ApiOperation("首页-能耗统计")
    public AjaxResult energyConsumption() {
        return AjaxResult.success(consumptionService.energyConsumption());
    }


    /**
     * x
     * （三生）设备能源监控
     */
    @PostMapping("/equipment")
    @ApiOperation("设备能源监控")
    public AjaxResult equipmentEnergy(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {

        List<EquipmentConsumptionDto> equipmentConsumptionDtos = consumptionService.selectequipmentEnergy(equipmentEnergyParam);
        return AjaxResult.success(new PageInfo<EquipmentConsumptionDto>(equipmentConsumptionDtos));
    }


    @PostMapping("/baseYear")
    @ApiOperation("设备能源同比")
    public AjaxResult baseYear(@RequestBody @Validated EquipmentEnergyBaseReq equipmentEnergyBaseReq) {

        List<EquipmentEnergyBaseResp> equipmentEnergyBaseRespList = consumptionService.baseYear(equipmentEnergyBaseReq);
        return AjaxResult.success(equipmentEnergyBaseRespList);
    }


    /**
     * 设备能源监控图表
     * <p>
     * 例如：12：00
     */
    @PostMapping("/equipment/chart")
    @ApiOperation("设备能源监控图表")
    public AjaxResult equipmentChartEnergy(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {
        return AjaxResult.success(consumptionService.equipmentChartEnergy(equipmentEnergyParam, 1));
    }


    /**
     * 车间能源监控
     */
    @PostMapping("/workshop")
    @ApiOperation("车间能源监控")
    public AjaxResult workshopEnergy(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {
        PageInfo pageInfo = new PageInfo<>(consumptionService.workshopEnergy(equipmentEnergyParam));
        return AjaxResult.success(pageInfo);
    }

    /**
     * 车间能源监控
     */
    @PostMapping("/workshop/chart")
    @ApiOperation("车间能源监控图表")
    public AjaxResult workshopChartEnergy(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {
        return AjaxResult.success(consumptionService.equipmentChartEnergy(equipmentEnergyParam, 1));
    }


    /**
     * 楼栋能源监控
     */
    @PostMapping("/building")
    @ApiOperation("楼栋能源监控")
    public AjaxResult buildingEnergy(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {
        PageInfo pageInfo = new PageInfo<>(consumptionService.buildingEnergy(equipmentEnergyParam));
        return AjaxResult.success(pageInfo);
    }

    /**
     * 楼栋能源监控图表
     */
    @PostMapping("/building/chart")
    @ApiOperation("楼栋能源监控图表")
    public AjaxResult buildingChart(@RequestBody EquipmentEnergyParam equipmentEnergyParam) {
        return AjaxResult.success(consumptionService.equipmentChartEnergy(equipmentEnergyParam, 2));
    }


    @PostMapping("/equipmentByReport")
    @ApiOperation("设备能源监控报表导出")
    public void equipmentDown(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = false) String begin, @RequestParam(required = false) String end, @RequestParam(required = false) Integer floor, @RequestParam(required = false) Integer dateType, HttpServletResponse response) throws IOException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        EquipmentEnergyParam equipmentEnergyParam = new EquipmentEnergyParam();
        equipmentEnergyParam.setPageNum(null);
        equipmentEnergyParam.setPageSize(null);
        equipmentEnergyParam.setBuildingId(buildingId);
        equipmentEnergyParam.setWorkshopIds(workshopIds);
        equipmentEnergyParam.setDeviceIds(deviceIds);
        equipmentEnergyParam.setClassifyId(classifyId);
        equipmentEnergyParam.setDeviceName(deviceName);
        if (begin != null && end != null) {
            equipmentEnergyParam.setBegin(sdf.parse(begin));
            equipmentEnergyParam.setEnd(sdf.parse(end));
        }
        equipmentEnergyParam.setFloor(floor);
        equipmentEnergyParam.setDateType(dateType);
        List<EquipmentConsumptionHead> result = consumptionService.selectequipmentDown(equipmentEnergyParam, 1);
        // 这里注意 请直接用浏览器或者用postman
        excelService.downloadList(EquipmentConsumptionHead.class, result, response, "设备能源监控报表");
    }


    @PostMapping("/workshopByReport")
    @ApiOperation("车间能源监控报表导出")
    public void workshopDown(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = false) String begin, @RequestParam(required = false) String end, @RequestParam(required = false) Integer floor, @RequestParam(required = false) Integer dateType, HttpServletResponse response) throws ParseException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        EquipmentEnergyParam equipmentEnergyParam = new EquipmentEnergyParam();
        equipmentEnergyParam.setPageNum(null);
        equipmentEnergyParam.setPageSize(null);
        equipmentEnergyParam.setBuildingId(buildingId);
        equipmentEnergyParam.setWorkshopIds(workshopIds);
        equipmentEnergyParam.setDeviceIds(deviceIds);
        equipmentEnergyParam.setClassifyId(classifyId);
        equipmentEnergyParam.setDeviceName(deviceName);
        if (begin != null && end != null) {
            equipmentEnergyParam.setBegin(sdf.parse(begin));
            equipmentEnergyParam.setEnd(sdf.parse(end));
        }
        equipmentEnergyParam.setFloor(floor);
        equipmentEnergyParam.setDateType(dateType);
        // 这里注意 请直接用浏览器或者用postman
        List<EquipmentConsumptionHead> result = consumptionService.selectequipmentDown(equipmentEnergyParam, 2);
        excelService.downloadList(EquipmentConsumptionHead.class, result, response, "车间能源监控报表");
    }

    @PostMapping("/buildingByReport")
    @ApiOperation("楼栋能源监控报表导出")
    public void buildingDown(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = false) String begin, @RequestParam(required = false) String end, @RequestParam(required = false) Integer floor, @RequestParam(required = false) Integer dateType, HttpServletResponse response) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        EquipmentEnergyParam equipmentEnergyParam = new EquipmentEnergyParam();
        equipmentEnergyParam.setPageNum(null);
        equipmentEnergyParam.setPageSize(null);
        equipmentEnergyParam.setBuildingId(buildingId);
        equipmentEnergyParam.setWorkshopIds(workshopIds);
        equipmentEnergyParam.setDeviceIds(deviceIds);
        equipmentEnergyParam.setClassifyId(classifyId);
        equipmentEnergyParam.setDeviceName(deviceName);
        if (begin != null && end != null) {
            equipmentEnergyParam.setBegin(sdf.parse(begin));
            equipmentEnergyParam.setEnd(sdf.parse(end));
        }
        equipmentEnergyParam.setFloor(floor);
        equipmentEnergyParam.setDateType(dateType);
        List<EquipmentConsumptionHead> result = consumptionService.selectequipmentDown(equipmentEnergyParam, 3);
        excelService.downloadList(EquipmentConsumptionHead.class, result, response, "楼栋能源监控报表");
    }

    /********************************* 老的************************************/


    @GetMapping("/purchase")
    @ApiOperation("查询能源采购管理")
    public AjaxResult purchase(@RequestParam(required = false) String type, @RequestParam(required = false) String begin, @RequestParam(required = false) String end, @RequestParam int currPage, @RequestParam int pageSize) {

        return equipmentConsumptionService.purchase(type, begin, end, currPage, pageSize);

    }

    @GetMapping("/purchase/chart")
    @ApiOperation("查询能源采购管理")
    public AjaxResult purchaseChart() {
        ArrayList<Double> purchaseMoney = new ArrayList<>();

        PurchaseChartDto purchaseChartDto = new PurchaseChartDto();

        purchaseChartDto.setAlikeCompare(265.78);
        purchaseChartDto.setCountPrice(3580.89);
        purchaseChartDto.setPrice(23.89);
        purchaseChartDto.setAnnulusCompare(89.72);
        purchaseMoney.add(0, 89.67);
        purchaseMoney.add(1, 8.65);
        purchaseMoney.add(2, 90.43);
        purchaseMoney.add(3, 68.12);
        purchaseMoney.add(4, 54.85);
        purchaseMoney.add(5, 256.23);
        purchaseMoney.add(6, 213.65);
        purchaseMoney.add(7, 134.24);
        purchaseMoney.add(8, 123.19);
        purchaseMoney.add(9, 89.35);
        purchaseMoney.add(10, 38.23);
        purchaseMoney.add(11, 12.67);
        purchaseChartDto.setPurchaseMoney(purchaseMoney);

        return AjaxResult.success(purchaseChartDto);
    }

    @PostMapping("/addPurchase")
    @ApiOperation("新增能源采购管理")
    public AjaxResult addPurchase(@RequestBody PurchasingEnergy purchasingEnergy) {
        return equipmentConsumptionService.addPurchase(purchasingEnergy);
    }

    @GetMapping("/deletePurchase")
    @ApiOperation("删除能源采购管理")
    public AjaxResult deletePurchase(@RequestParam Integer id) {
        return equipmentConsumptionService.deletePurchase(id);
    }

    @PostMapping("/updatePurchase")
    @ApiOperation("更新能源采购管理")
    public AjaxResult updatePurchase(@RequestBody PurchasingEnergy purchasingEnergy) {
        return equipmentConsumptionService.updatePurchase(purchasingEnergy);
    }


    /**
     * 采购金额统计 ,移动端采购金额统计 接口
     *
     * @return
     */
    @PostMapping("/purchaseMoneyStat")
    @ApiOperation("采购金额统计")
    public AjaxResult purchaseMoneyStat() {
        return equipmentConsumptionService.purchaseMoneyStat();
    }

    @PostMapping("/workshop/contrast")
    @ApiOperation("车间能耗对比")
    public AjaxResult workshopContrast(@ApiParam(name = "workDateStatistics", value = "车间能耗对象") @RequestBody WorkDateStatistics workDateStatistics) {
        Map<String, Double> statistics = new HashMap<>();

        statistics.put("bigPeak", 3220.30);
        statistics.put("smallPeak", 1.89);
        statistics.put("average", 1250.20);
        statistics.put("mesoPosition", 2503.52);
        statistics.put("difference", 26.89);
        statistics.put("total", 38926.69);

        return AjaxResult.success("查询成功", statistics);
    }

    /*@PostMapping("/equipmentByReport/overhaulDownload")
    @ApiOperation("设备模板报表导出")
    public void equipmentByReport(
            @RequestBody EquipmentEnergyParam equipmentEnergyParam,
            HttpServletResponse response
    ) throws IOException {
        // 这里注意 请直接用浏览器或者用postman
        List<TemplateEnergy> equipment =  energyTemplateService.findTemplateEnergy(1,1);
        System.out.println(equipment.toString());
        excelService.download(TemplateEnergy.class, equipment, response);
    }*/


    @PostMapping("/equipmentByReport1")
    @ApiOperation("设备能源监控报表导出")
    public void equipmentByReport(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = true) String begin, @RequestParam(required = true) String end, @RequestParam(required = false) Integer floor, HttpServletResponse response) throws IOException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 这里注意 请直接用浏览器或者用postman
        List<EquipmentConsumptionDto> equipment = overallEnergyService
                .equipmentByReport(pageNum, pageSize, buildingId, workshopIds, deviceIds, classifyId, deviceName, sdf.parse(begin), sdf.parse(end), floor)
                .stream()
                .collect(Collectors.toList());
        excelService.download(EquipmentConsumptionDto.class, equipment, response);
    }

    /*@PostMapping("/equipmentByReport")
    @ApiOperation("设备能源监控报表导出")
    public void equipmentByReport(
            @RequestBody EquipmentEnergyParam equipmentEnergyParam,
            HttpServletResponse response
    ) throws IOException {
        // 这里注意 请直接用浏览器或者用postman
        List<EquipmentConsumptionDto> equipment =  overallEnergyService.equipmentByReport(
                1,
                100,
                equipmentEnergyParam.getBuildingId(),
                equipmentEnergyParam.getWorkshopIds(),
                equipmentEnergyParam.getDeviceIds(),
                equipmentEnergyParam.getClassifyId(),
                equipmentEnergyParam.getDeviceName(),
                equipmentEnergyParam.getBegin(),
                equipmentEnergyParam.getEnd());

        System.out.println("1111111111111111111111");
        Double a =2.00;
        System.out.println(String.valueOf(a));

        List<EquipmentConsumptionsDto> dtos = equipment.stream().map(d -> new EquipmentConsumptionsDto(
                d.getId(), d.getEquipmentId(), d.getDeviceName(), String.valueOf(d.getEquipmentConPrice()), String.valueOf(d.getEquipmentCon()), String.valueOf(d.getEquipmentPowerPrice()),
                String.valueOf(d.getPrice()), String.valueOf(d.getUnitEnergy()), d.getType(), d.getCreatedBy(), d.getCreatedDate(), d.getLastModifiedDate(), String.valueOf(d.getCoolingType()),
                String.valueOf(d.getTapType()), String.valueOf(d.getSofteningType()), String.valueOf(d.getCompressType()), String.valueOf(d.getNaturalType()), String.valueOf(d.getVapourType()), String.valueOf(d.getSpikesType()), String.valueOf(d.getPeaksType()),
                String.valueOf(d.getParityType()), String.valueOf(d.getLowebbType()), String.valueOf(d.getLnstrumentAirPressure()), String.valueOf(d.getCleanCompressedAir()), String.valueOf(d.getAirHotEnergy()), String.valueOf(d.getPowerConsumption()),
                String.valueOf(d.getWaterConsumption()), String.valueOf(d.getGasConsumption()), String.valueOf(d.getActivePower()), String.valueOf(d.getReactivePower()), String.valueOf(d.getTotalDosage())
        )).limit(1).collect(Collectors.toList());
        System.out.println(dtos);

        excelService.download(EquipmentConsumptionsDto.class, dtos, response);
        //excelService.download(EquipmentConsumptionDto.class, equipment.stream().limit(1).collect(Collectors.toList()), response);
    }*/

    @PostMapping("/workshopByReport1")
    @ApiOperation("车间能源监控报表导出")
    public void workshopByReport(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = true) String begin, @RequestParam(required = true) String end, @RequestParam(required = false) Integer floor, HttpServletResponse response) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 这里注意 请直接用浏览器或者用postman
        List<EquipmentConsumptionDto> equipment = overallEnergyService.workshopByReport(pageNum, pageSize, buildingId, workshopIds, deviceIds, classifyId, deviceName, sdf.parse(begin), sdf.parse(end), floor);
        excelService.download(EquipmentConsumptionDto.class, equipment, response);
    }

    @PostMapping("/buildingByReport1")
    @ApiOperation("楼栋能源监控报表导出")
    public void buildingByReport(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Long buildingId, @RequestParam(required = false) List<Integer> workshopIds, @RequestParam(required = false) List<Integer> deviceIds, @RequestParam(required = false) Integer classifyId, @RequestParam(required = false) String deviceName, @RequestParam(required = true) String begin, @RequestParam(required = true) String end, @RequestParam(required = false) Integer floor, HttpServletResponse response) throws IOException, ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 这里注意 请直接用浏览器或者用postman
        List<EquipmentConsumptionDto> equipment = overallEnergyService.buildingByReport(pageNum, pageSize, buildingId, workshopIds, deviceIds, classifyId, deviceName, sdf.parse(begin), sdf.parse(end), floor);
        excelService.download(EquipmentConsumptionDto.class, equipment, response);
    }


//    /**
//     * 能耗报表
//     */
//    @GetMapping("/equipmentreport")
//    @ApiOperation("能耗报表查询")
//    public AjaxResult selectenquipmentConsumptionBycondition(Long workshopId,Long energyType) throws IOException
//    {
//        return equipmentConsumptionService.selectenquipmentConsumptionBycondition();
//    }


    @PostMapping("/consumptionProportion")
    public AjaxResult consumptionProportion() {
        return AjaxResult.success(consumptionService.consumptionProportion2());
    }

    @PostMapping("/consumptionProportion2")
    public AjaxResult consumptionProportion2() {
        return AjaxResult.success(consumptionService.consumptionProportion2());
    }
}
